% ### Step 10: Calculate Indirect Effects (Column Sums)
% **File:** `2_3_Indirect_effects.m`  
% **Language:** MATLAB  
% **Description:** Calculates firm-level measures of indirect effects using spatial econometric techniques.
% - **Inputs:**
%   - __compustat_cleaned_allVars2 (from Step 4)
%   - SCnetwork2 
% - **Outputs:**
%   - SC_column_sums
% - **IMPORTANT NOTE: Run the following Stata code to generate and save a Stata dataset with the Indirect effects (column sums):**
%   ```stata
%   import delimited "SC_column_sums.csv", clear
%   rename columnsum columnsum_all
%   winsor2 columnsum_all˜
%   drop index
%   save "SC_column_sums", replace
%   ```

% ---------------- PATHS  ---------------- %
clear all

base = '/Users/path';
BaseDir = fullfile('path');
global data_dir;
data_dir = 'your_path';
global table_dir;
table_dir = 'your_path';

addpath(genpath(fullfile(BaseDir,'SpatialModels')));

% Load the necessary tables
% NOTE: use "__compustat_cleaned_allVars2.csv" to generate the indirect effects and merge them back to your final dataset ("__compustat_cleaned.csv")
datafname = fullfile(BaseDir, 'compustat_cleaned_allVars2.csv');
SCname    = fullfile(BaseDir, 'SCnetwork2.csv');
SCW       = readtable(SCname);
SrcData   = readtable(datafname);

% Set dependent variable and regressors
yvar  = 'capx_at';
xvars = {'l_logSale','l_cash','l_zscore','l_roa','l_mb','l_bl'};

% Remove rows with missing values and keep fyear >= 2003
tmp = [SrcData{:, yvar}, SrcData{:, xvars}];
SrcData = SrcData(~any(isnan(tmp),2), :);
myData  = SrcData(SrcData.fyear >= 2003,:);

% Define variable names for SAR estimation
VarNames.idpair = {'gvkey1','gvkey2'};
VarNames.wscore = 'score';
VarNames.idvar  = 'gvkey';
VarNames.tvar   = 'fyear';
VarNames.yvar   = yvar;
VarNames.xvars  = xvars;

% Estimate the SAR model (sarres must contain fields W, IDS, and rho_out)
% Set number of workers for parallel processing
if isempty(gcp('nocreate'))
    parpool('local', 4);
end
sarres = EstimateSAR(myData, SCW, VarNames);

% Loop over years to compute the column sums from the spatial multiplier
years    = sort(unique(myData.fyear));
Z_concat = cell2table(cell(0,4), 'VariableNames', {'gvkey','index','columnsum','fyear'});
for i = 1:length(years)
    year = years(i);
    n    = size(sarres.W{i},1);
    colsum = (eye(n) - sarres.rho_out(1,3)*sarres.W{i})' \ ones(n,1);
    ids    = table2array(sarres.IDS{i});
    Z      = array2table([ids, colsum, repmat(year, n, 1)], ...
              'VariableNames', {'gvkey','index','columnsum','fyear'});
    Z_concat = [Z_concat; Z];
end

outputFile = fullfile(base, t_dir, 'SC_column_sums.csv');
writetable(Z_concat, outputFile);